package com.bank.controller;

import com.bank.api.model.AmountExchange;
import com.bank.api.model.PriceUnit;
import com.bank.service.TransferService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/bank")
public class TransferController {


    @Autowired
    private TransferService transferService;


    // http://127.0.0.1:8088/bank/transfer?userId=10086&&exchangeAmount=100&&type=rmb &trxNo=test1
    @RequestMapping("/transfer")
    public Map<String, Object> transfer(Long userId, Long exchangeAmount, String type, String trxNo) {
        Map<String, Object> result = new HashMap<>();
        AmountExchange amountExchange = new AmountExchange();
        amountExchange.setExchangeAmount(exchangeAmount);
        amountExchange.setUserId(userId);
        amountExchange.setFrom(type.equals("rmb") ? PriceUnit.RMB : PriceUnit.DOLLAR);
        amountExchange.setTo(amountExchange.getFrom() == PriceUnit.RMB ? PriceUnit.DOLLAR : PriceUnit.RMB);
        amountExchange.setTrxRecordNo(trxNo);
        int transfer = transferService.transfer(amountExchange);
        System.out.println(transfer);
        result.put("data", transfer);
        return result;
    }

}
